package com.java.jdbc;

import javax.swing.plaf.nimbus.State;
import java.sql.*;
import java.util.Scanner;

public class LoginDemo2 {
    public static final String URL = "jdbc:mysql://localhost:3306/testdb1?serverTimezone=GMT&useUnicode=true&characterEncoding=Utf-8&useSSL=false";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "123456";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        try(Connection connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "select * from t_user where username =? and password =?");) {
            preparedStatement.setString(1, username);
            // 设置第二个参数（对应SQL语句中的第二个? 占位符），即密码
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                System.out.println("登陆成功！进入主界面");
            }else {
                System.out.println("账号或密码错误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
